package com.xiaoxx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoxx.common.error.ErrorException;
import com.xiaoxx.common.result.RType;
import com.xiaoxx.mapper.ChatModelMapper;
import com.xiaoxx.model.dto.chat.ChatModelDto;
import com.xiaoxx.model.dto.chat.ChatModelQueryDto;
import com.xiaoxx.model.entity.chat.ChatModel;
import com.xiaoxx.model.vo.chat.ChatApiKeyVo;
import com.xiaoxx.model.vo.chat.ChatModelVo;
import com.xiaoxx.service.ChatApikeyService;
import com.xiaoxx.service.ChatModelService;
import com.xiaoxx.utils.BeanDtoVoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author :HX
 * @date : 2023/11/29
 */
@Service
public class ChatModelServiceImpl extends ServiceImpl<ChatModelMapper, ChatModel> implements ChatModelService {
    @Autowired
    private ChatApikeyService chatApikeyService;
    @Override
    public ChatModel getModelByContentType(Integer contentType) {
        ChatModel chatModel = baseMapper.getModelByContentType(contentType);
        return chatModel;
    }

    @Override
    public Boolean addChatModel(ChatModelDto chatModelDto) {
        ChatModel chatModel = new ChatModel();
        chatModel.setContentType(chatModelDto.getContentType());
        chatModel.setModel(chatModelDto.getModel());
        chatModel.setModelDesc(chatModelDto.getModelDesc());
        chatModel.setLimitRule(chatModelDto.getLimitRule());
        chatModel.setLimitNum(chatModelDto.getLimitNum());
        chatModel.setIsCache(chatModelDto.getIsCache());
        return save(chatModel);
    }

    @Override
    public Boolean updChatModel(ChatModelDto chatModelDto) {
        ChatModel convert = BeanDtoVoUtil.convert(chatModelDto, ChatModel.class);
        return updateById(convert);
    }

    @Override
    public IPage<ChatModelVo> getList(ChatModelQueryDto chatModelQuery) {
        List<ChatApiKeyVo> list = chatApikeyService.getList(null);
        Map<String, String> collect = list.stream().collect(Collectors.toMap(ChatApiKeyVo::getId, i -> i.getApiKey()));
        Page<ChatModel> page = page(new Page<ChatModel>(chatModelQuery.getCurrent(), chatModelQuery.getSize()), new LambdaQueryWrapper<ChatModel>()
                .like(StringUtils.hasLength(chatModelQuery.getModel()), ChatModel::getModel, chatModelQuery.getModel())
                .orderByDesc(ChatModel::getCreateTime));
       page.getRecords().forEach(i -> {
            if (collect.containsKey(i.getApikeyId())) {
                i.setApiKey(collect.get(i.getApikeyId()));
            }
        });
        return BeanDtoVoUtil.pageVo(page, ChatModelVo.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updStatus(String id) {
        ChatModel chatModel = getById(id);
        if (chatModel == null || chatModel.getContentType() == null) {
            throw new ErrorException(RType.SYS_ERROR_CODE_500);
        }
        if (chatModel.getStatus() == 1) {
            //未使用
            update(new LambdaUpdateWrapper<ChatModel>()
                    .set(ChatModel::getStatus, 2)
                    .eq(ChatModel::getId, id));
        } else {
            //使用中
            boolean update = update(new LambdaUpdateWrapper<ChatModel>()
                    .set(ChatModel::getStatus, 1)
                    .eq(ChatModel::getId, id));
            if (update) {
                update(new LambdaUpdateWrapper<ChatModel>()
                        .set(ChatModel::getStatus, 2)
                        .ne(ChatModel::getId, id)
                        .eq(ChatModel::getContentType, chatModel.getContentType()));
            }
        }
        return true;
    }

    @Override
    public Boolean del(String id) {
        return removeById(id);
    }
}
